package ru.st.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

public class Login_test_old extends ru.st.selenium.pages.TestBase_old {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  
  @Test
  public void testLogin() throws Exception {
    driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.name("password")).sendKeys("admin");
    driver.findElement(By.name("submit")).click();

  }
  
  
  @Test(dependsOnMethods={"testLogin"})
  public void testAddFilmFalseData() throws Exception {
	driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
    driver.findElement(By.name("name")).clear();
    driver.findElement(By.name("name")).sendKeys("Add_film_for_test");
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
    driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("Add_film_for_test"+Keys.RETURN);
    
    if (isElementPresent(By.xpath(".//*[text()='No movies where found.']/.."))){
    	driver.findElement(By.id("q")).clear();
    	driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
    }
    
  }
  
  
  @Test(dependsOnMethods={"testLogin"})
  public void testAddFilm() throws Exception {
	driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
    driver.findElement(By.id("imdbsearch")).clear();
    driver.findElement(By.id("imdbsearch")).sendKeys("inception");
    driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
    driver.findElement(By.linkText("Inception")).click();
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
  }
  
  
  @Test(dependsOnMethods={"testLogin"})
  public void testDeleteFilm() throws Exception {
	driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.cssSelector("div.title")).click();
    driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$"));
  }
  

  @Test(dependsOnMethods={"testLogin"})
  public void testFindFilm() throws Exception {
	driver.get(baseUrl + "/php4dvd/");
	driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
	driver.findElement(By.name("name")).clear();
	driver.findElement(By.name("name")).sendKeys("test 1");
	driver.findElement(By.name("year")).clear();
	driver.findElement(By.name("year")).sendKeys("2013");
	driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
	driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
    driver.findElement(By.name("name")).clear();
    driver.findElement(By.name("name")).sendKeys("hobbit");
    driver.findElement(By.name("year")).clear();
    driver.findElement(By.name("year")).sendKeys("2010");
    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
    ///////////////////////////////////////////////////////////////////////
    driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("test"+Keys.RETURN);
    
    if (isElementPresent(By.className("title"))){
    	driver.findElement(By.id("q")).clear();
    	driver.findElement(By.id("q")).sendKeys("matrix"+Keys.RETURN);
    }

    if (isElementPresent(By.xpath(".//*[text()='No movies where found.']/.."))){
    	driver.findElement(By.id("q")).clear();
    	driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
    }
    
    driver.findElement((By.xpath(".//*[text()='test 1']/.."))).click();
    driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$"));
    driver.get(baseUrl + "/php4dvd/");
    
    driver.findElement((By.xpath(".//*[text()='hobbit']/.."))).click();
    driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$"));
    driver.get(baseUrl + "/php4dvd/");
    
  }
  

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}